package com.sais.manage.boot.cms.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sais.common.entity.Result;
import com.sais.manage.boot.cms.dto.ArticleDTO;
import com.sais.manage.boot.cms.dto.ArticleDetailsDTO;
import com.sais.manage.boot.cms.dto.ArticleSaveDTO;
import com.sais.manage.boot.cms.dto.ArticleSearchDTO;
import com.sais.manage.boot.cms.service.IArticleService;
import com.sais.manage.boot.platform.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 栏目-文章管理 controller
 *
 * @author zwp
 * @date 2020/02/16
 **/
@Api(tags = "文章 管理")
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/cms/article")
public class ArticleController {

    @Resource
    private IArticleService articleService;


    @ApiOperation("保存或者修改文章")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result saveArticle(@RequestBody ArticleSaveDTO articleSaveDTO) {
        articleService.saveArticle(articleSaveDTO);
        return new Result();
    }


    @ApiOperation("根据业务主键删除 文章")
    @RequestMapping(value = "/delete/{articleId}", method = RequestMethod.GET)
    public Result deleteArticle(@PathVariable("articleId") String articleId) {
        articleService.deleteArticle(articleId);
        return new Result();
    }


    @ApiOperation("根据业务主键查询 文章")
    @RequestMapping(value = "/select/{articleId}", method = RequestMethod.GET)
    public Result selectArticle(@PathVariable("articleId") String articleId) {
        ArticleDTO article = articleService.selectArticle(articleId);
        return new Result().data("article", article);
    }

    @ApiOperation("根据业务主键查询 文章")
    @RequestMapping(value = "/select/details/{articleId}", method = RequestMethod.GET)
    public Result selectArticleDetails(@PathVariable("articleId") String articleId) {
        ArticleDetailsDTO article = articleService.selectArticleDetails(articleId);
        return new Result().data("article", article);
    }


    @ApiOperation("模糊分页查询 查询条件为空时分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result listArticle(ArticleSearchDTO searchDTO,
                              @RequestParam(name = "current", defaultValue = "1") Integer current,
                              @RequestParam(name = "size", defaultValue = "30") Integer size) {
        Page<ArticleDTO> page = articleService.listArticle(searchDTO, current, size);
        return new Result().data("articles", page);
    }

    @ApiOperation("查询 当前用户发布的消息")
    @RequestMapping(value = "/list/user", method = RequestMethod.GET)
    public Result listArticleByCurrentUserId() {
        String userId = ShiroUtils.getLoginUser().getUkId();
        List<ArticleDTO> articles = articleService.listArticleByCurrentUserId(userId);
        return new Result().data("articles",articles);
    }

    @ApiOperation("更新文章 状态")
    @RequestMapping(value = "/update/status/{articleId}", method = RequestMethod.GET)
    public Result updateArticleStatus(@PathVariable("articleId") String articleId){
        articleService.updateArticleStatus(articleId);
        return Result.response();
    }


}
